package com.bz.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bz.dao.BusinessScopeDao;
import com.bz.entity.BusinessScopeEntity;
import com.bz.model.BusinessScopeDetailDto;
import com.bz.model.BusinessScopeDto;
import com.bz.service.IBusinessScopeService;

@Service
public class BusinessScopeServiceImpl implements IBusinessScopeService {

    @Autowired
    private BusinessScopeDao businessScopeDao;

    @Override
    public List<BusinessScopeDto> list() {
        // 业务范围，1：检测，2：报告
        List<BusinessScopeDto> list = new ArrayList<>();
        BusinessScopeDto bd1 = new BusinessScopeDto();
        bd1.setType("检测");
        BusinessScopeDto bd2 = new BusinessScopeDto();
        bd2.setType("报告");
        list.add(bd1);
        list.add(bd2);

        LambdaQueryWrapper<BusinessScopeEntity> query = new LambdaQueryWrapper<>();
        query.orderByDesc(BusinessScopeEntity::getSort).orderByDesc(BusinessScopeEntity::getUpdateDate);
        List<BusinessScopeEntity> datas = businessScopeDao.selectList(query);
        if (CollectionUtils.isEmpty(datas)) {
            return list;
        }

        bd1.setDatas(datas.stream().filter(it -> {
            return StringUtils.equals("1", it.getType());
        }).map(it -> {
            BusinessScopeDetailDto d = new BusinessScopeDetailDto();
            d.setImg(it.getImg());
            d.setTitle(it.getTitle());
            return d;
        }).collect(Collectors.toList()));
        bd2.setDatas(datas.stream().filter(it -> {
            return StringUtils.equals("2", it.getType());
        }).map(it -> {
            BusinessScopeDetailDto d = new BusinessScopeDetailDto();
            d.setImg(it.getImg());
            d.setTitle(it.getTitle());
            return d;
        }).collect(Collectors.toList()));

        return list;
    }

}
